//
//  AppDelegate.m
//  黑马微博-wzy
//
//  Created by Mac on 2016/11/20.
//  Copyright © 2016年 Mac. All rights reserved.
//
//=============================================================
//    NSLog(@"%@", kCFBundleVersionKey);//得到:CFBundleVersion
//(2)Foundation 转化为Core Foundation 类型
//NSString *test = @"哈哈哈哈";
//CGPDFStringRef str = (__bridge CFStringRef)test;

//NSString/NSArray/NSDictionary           Foundation 框架
//CFStringRef/CFArrayRef/CFDictionaryRef  Core Foundation 框架
//Foundation和Core Foundation 数据类型之间的转化 使用桥接



#import "AppDelegate.h"
#import "HMTabBarViewController.h"
//新特性:第一次使用该版本
#import "HMNewFeatureViewController.h"
//OAuth2.0 授权
#import "HMOAuthViewController.h"
#import "HMControllerTool.h"
#import "HMAccountTool.h"
//使用 AF监听网络
#import "AFNetworking.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //1/创建窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    //2/显示窗口(成为主窗口)
    [self.window makeKeyAndVisible];
    
    //3/设置窗口的根控制器

    //如果上次登录成功,则不需再次登录
    //读取账号信息
    HMAccount *account = [HMAccountTool account];
    
    if (account){
        //如果account 不为空,说明上次授权登录成功,无需再次登录
        [HMControllerTool chooseRootViewController];
    
    }else{
       //
      self.window.rootViewController = [[HMOAuthViewController alloc] init];
    }
    
    
    
    
    //监听网络状态
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    //当网络状态发生变化就会调用
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        /*
         typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
         AFNetworkReachabilityStatusUnknown          = -1,
         AFNetworkReachabilityStatusNotReachable     = 0,
         AFNetworkReachabilityStatusReachableViaWWAN = 1,
         AFNetworkReachabilityStatusReachableViaWiFi = 2,
         };
         */
        
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                HMLog(@"未知网络");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                HMLog(@"没有网络(断网)");
                [MBProgressHUD showError:@"网络异常,请检查网络设置!"];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                HMLog(@"手机自带网络");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                HMLog(@"WiFi");
                break;
            default:
                break;
        }
        
    }];
    //开始监听
    [mgr startMonitoring];
    
    //
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    
    if (version >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

//程序进入后台的时候调用
- (void)applicationDidEnterBackground:(UIApplication *)application {
    //提醒操作系统:当前这个应用程序需要在后台开启一个任务
    //操作系统会允许这个应用程序在后台保持运行状态(能够持续的时间是不确定的)
    UIBackgroundTaskIdentifier taskID = [application beginBackgroundTaskWithExpirationHandler:^{
        //Expiration 有效期
        //后台运行的有效期到了的时候,就会自动调用下面方法
        [application endBackgroundTask:taskID];
        
    }];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - 当出现内存告急的时候 使用
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
  //赶紧清除所有的内存缓存
    [[SDImageCache sharedImageCache] clearMemory];
    //赶紧停止正在进行的图片下载操作
    [[SDWebImageManager sharedManager] cancelAll];

}

@end
